<html>
	<?php
		require_once("classes/User.php");
		require_once("classes/Database.php");
		require_once("classes/Head.php");
		require_once("classes/Section.php");
		require_once("classes/Image.php");
		require_once("classes/Title.php");
		require_once("classes/Paragraph.php");
		require_once("classes/LinkList.php");
		require_once("classes/Table.php");
		require_once("classes/EntryList.php");
		require_once("classes/Form.php");
		
		session_start();
		
		$head = new Head();
		$user = $_SESSION["user"];
		
		if($_SESSION["login"]){
			$title = "Ver Anuncio";
		}else{
			$title = "Login Errado";
		}
		
		if(isset($_GET["ad"])){
			$ad = $_GET["ad"];
		}else{
			$ad = NULL;
		}
		
		$head->addLayout("css/style.css");
		$head->addTitle($title);
		
		$head->printHead();
	?>
	<body id="body">
		<div id="struture">
			<div id="container">
		        <?php
		        	$header = new Section("header");					
						$header->addElement(new Image("images/job.png", "logo", "100px", "100px"));
						
						$title = new Title("Ofertas de Emprego", "title");
						$header->addElement($title);
					$header->printSection();

					$db = new Database("localhost", "root", "");
					$db->connect("emprego");
					if(isset($ad)){
						$db->executeQuery("SELECT a.title, a.description, u.nome as enterprise, l.name as loc, c.name as cat, a.salary, a.savedate, a.owner, a.id
											FROM announcement a, users u, location l, category c
											WHERE a.owner = u.id AND a.location = l.id AND a.category = c.id AND a.id = $ad");
						$line = $db->getResultLine(MYSQL_ASSOC);
					}
					
		        	/* Left Page */
		        	$leftPage = new Section("leftPage");
					if(!$_SESSION["login"]){
						$leftPage->addElement(new Title("Erro", "menuTitle"));
						$leftPage->addElement(new Paragraph("Login Errado", "menu"));
					}else{
						if(!$line){
							$leftPage->addElement(new Title("Erro", "menuTitle"));
							$leftPage->addElement(new Paragraph("Este anuncio n&atildeo foi encontrado da base de dados.", "menu"));
						}else{
							$leftPage->addElement(new Title("Anuncio", "menuTitle"));
							if($user->isThisRole("candidate") || ($user->isThisRole("enterprise") && $line["owner"] != $user->getId())){
								$leftPage->addElement(new Paragraph("Este emprego &eacute interesante para si?</br>
																	Coloque um post ou envie um e-mail &aacute empresa.", "menu"));
								$voltar = new LinkList("menuEntry");
								$voltar->addLink("Logout", "scripts/logout.php", new Image("images/leave.png", "icon", "16px", "16px"));
								$voltar->addLink("Voltar", "home.php", new Image("images/leave.png", "icon", "16px", "16px"));
								$leftPage->addElement($voltar);
							}else if($user->isThisRole("enterprise"  && $line["owner"] == $user->getId())){
								$leftPage->addElement(new Paragraph("Confira os dados do seu anuncio, comente um post, altere ou elimine o anuncio.", "menu"));

								$voltar = new LinkList("menuEntry");
								$voltar->addLink("Logout", "scripts/logout.php", new Image("images/leave.png", "icon", "16px", "16px"));
								$voltar->addLink("Voltar", "home.php", new Image("images/leave.png", "icon", "16px", "16px"));
								$leftPage->addElement($voltar);

								$leftPage->addElement(new Title("Gerir Anuncio", "menuTitle"));
								$gest = new LinkList("menuEntry");
								$_SESSION["ad"] = $ad;
								$gest->addLink("Remover Anuncio", "scripts/removeAnnouncement.php", new Image("images/rm.png", "icon", "16px", "16px"));
								$leftPage->addElement($gest);
							}
						}
					}
					$leftPage->printSection();

					$content = new Section("content");
					if($_SESSION["login"]){
						if(!$line){
							$content->addElement(new Title("Erro", "seccaoTitle"));
						}else{
							$content->addElement(new Title($line["title"], "seccaoTitle"));
							
							$enterprise = $line["enterprise"];
							$db->executeQuery("SELECT email FROM users WHERE nome='$enterprise'");
							$line2 = $db->getResultLine(MYSQL_ASSOC);
							$entry = new EntryList("entry");
							$entry->openEntry();
							$entry->addTitle("Dados da Empresa");
							$entry->addEntry("Empresa:", $enterprise);
							$entry->addEntry("E-mail:", $line2["email"]);
							$entry->closeEntry();
							
							$entry->openEntry();
							$entry->addTitle("</br>Dados do Anuncio");
							$entry->addEntry("Localiza&ccedil&atildeo:", $line["loc"]);
							$entry->addEntry("Categoria:", $line["cat"]);
							$entry->addEntry("Data:", $line["savedate"]);
							$entry->addEntry("Sal&aacuterio:", $line["salary"]." Euro(s)");
							$entry->closeEntry();
							$content->addElement($entry);
							
							$content->addElement(new Title("</br>Anuncio", "simpleTitle"));
							$content->addElement(new Paragraph($line["description"], "seccao"));
							
							$post = new Form("scripts/validatePost.php", "POST", "formTable");
							$post->addLabel("ad", $ad);
							$post->addTextArea("Envie um post:", "inputarea", "post", "", 5, 80, TRUE);
							$post->addInput("", "submit", "", "button", "Postar", "", FALSE);
							$content->addElement($post);
							
							$db->executeQuery("SELECT u.nome as user, p.save_date, p.content, u.id as id, p.id as post
												FROM posts p JOIN users u ON p.candidate = u.id
												WHERE p.announcement = '$ad'
												ORDER BY p.save_date DESC");
							$entryPost = new EntryList("entry");
							while($line = $db->getResultLine(MYSQL_ASSOC)){
								$entryPost->openEntry();
								$entryPost->addTitle("</br>".$line["user"]." ---- ".$line["save_date"]);
								$entryPost->addEntry("", $line["content"]);
								if($line["id"] == $user->getId()){
									$_SESSION["postRemove"] = $line["post"];
									$_SESSION["ad"] = $ad;
									$entryPost->addLinkTitle("scripts/removePost.php", "Remover");
								}
								$entryPost->closeEntry();
							}
							$content->addElement($entryPost);
						}
					}else{
						$content->addElement(new Title("Erro", "seccaoTitle"));
					}
					
					$content->printSection();
					
					$footer = new Section("footer");
						$text = new Paragraph("Copyright&copy Nuno Pombo", "");
						$footer->addElement($text);
					$footer->printSection();
		        ?>
			</div>
		</div>
	</body>
</html>